package org.example.slave.util;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import lombok.experimental.UtilityClass;
import org.example.slave.packet.HeaderPacket;

/**
 * @author dingrui
 * @since 2022/2/16
 * @description
 */
@UtilityClass
public class PacketMgr {

    public void writeBody(Channel ch, byte[] body) {
        writeBody(ch, body, (byte) 0);
    }

    public void writeBody(Channel ch, byte[] body, byte packetSeqNumber) {
        HeaderPacket header = new HeaderPacket();
        header.setPacketBodyLength(body.length);
        header.setPacketSequenceNumber(packetSeqNumber);
        writeBody0(ch, header.toBytes(), body);
    }

    public void writeBody0(Channel ch, byte[]... buf) {
        ch.writeAndFlush(Unpooled.copiedBuffer(buf));
    }
}
